<?php
$isReloadable = true;
require_once("../../pers/inc.config.php");
require_once(R2_FS_WEB_ROOT.'/app/_inc/inc.session.php');
require_once(R2_FS_WEB_ROOT.'/app/_inc/funct.html.php');
require_once(R2_FS_WEB_ROOT.'/app/util/_inc/funct.confDb.php');

$sTesto = '';
if (reqVar('azione') == 'verifica') {
	$msgPag = verifica($sTesto);
}

$sCmdR  = "&nbsp;&nbsp;&nbsp";
$sCmdR .= "<a class='cmd1' href=\"javascript:document.frm1.azione.value='verifica';document.frm1.submit();\" >"
	."verifica</a>";
$sCmdR .=  cmdTorna();
include(R2_FS_WEB_ROOT."/app/util/_inc/inc.aMenuPag.php");
testaHtml('', $msgPag, '30.10.10', $aMenuPag);
?>
<script type="text/javascript">
function confAgg() {
	if (confirm("Confermi l'aggiornamento della struttura del data-base ?")) {
		document.frm1.azione.value='aggiorna';
		document.frm1.submit();
	}
}
</script>
<table border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <tr>
      <td class="cmd1" align="right" valign='bottom'>
         <? echo $sCmdR;?>
      </td>
   </tr>
</table>
<form name='frm1' action='<?=$_SERVER['PHP_SELF']?>' method='post' enctype="multipart/form-data">
<input type='hidden' name='azione' value="confronta">
<input type='hidden' name='xsn' value="<?=XSN?>">
<table class="tab1" border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
	<tr>
		<td class="tit1" colspan='5' >Verifica ore</td>
	</tr>
	<tr>
		<td class="lab1" width="10%">Mese</td>
		<td class="txt1" width="10%">
			<?php 
			reqDef('mese', date('m',time()-20*24*3600));
			$aMesi = array(1=>'gennaio',2=>'febbraio',3=>'marzo',4=>'aprile',5=>'maggio',6=>'giugno'
				,7=>'luglio',8=>'agosto',9=>'settembre',10=>'ottobre',11=>'novembre',12=>'dicembre');
			echo getHtmlSelect('mese',$aMesi);?>
		</td>
		<td class="lab1" width="10%">Anno</td>
		<td class="txt1" width="20%">
			<?php 
			reqDef('anno', date('Y',time()-20*24*3600));
			$aAnni = array();reqVar('anno');
			for($aaaaX=reqVar('anno')-3; $aaaaX<reqVar('anno')+3; $aaaaX++) {
				$aAnni[$aaaaX] = $aaaaX;
			}
			echo getHtmlSelect('anno',$aAnni);?>
		</td>
		<td class="txt1" width="50%"><b>&nbsp;Data consolidamento&nbsp;:&nbsp;
			<?php echo YMD2DMY(R2_DATA_CONSOLID);?></b>
		</td>
	</tr>
	<tr>
		<td class="txt1" colspan='5'>
			<pre class='txt1'><?php echo $sTesto;?></pre>
		</td>
	</tr>
</table>
</form>
<table border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <tr>
      <td class="cmd1" align="right" valign='bottom'>
         <? echo $sCmdR;?>
      </td>
   </tr>
</table>
<?
codaHtml();
exit();

function verifica(&$sTesto) {
	global $objDB, $objSess, $aUtentiRint;
	$sTesto = '';
	$sDataIniz = reqVar('anno').'-'.reqVar('mese').'-01';
	$odx = new rint2Date();
	if ($odx->setYMD($sDataIniz)===false) {
		return 'mese errato<br>';
	} else {
		$dataIniz = $odx->getUnix();
		$sDataFine = $odx->monthLast('ymd');
		$dataFine = $odx->monthLast('unix');
	}
	$sTesto = "$sDataIniz / $sDataFine\r\n";
	$sSql = "SELECT a.id_operatore, a.data, sum(ore) AS oreg FROM attivita a " .
			" WHERE a.data BETWEEN '".date('Y-m-d',$dataIniz)."' AND '".date('Y-m-d',$dataFine)."'" .
			" GROUP BY a.id_operatore, a.data".
			" ORDER BY a.id_operatore, a.data ";
	$objDB->query($sSql);
	$aOre = array();
	$tot = 0;
	while($ac = $objDB->fetch()) {
		$aOre[$ac['id_operatore']][$ac['data']] = $ac['oreg'];
		$tot += $ac['oreg'];
	}
	$aFest = array('01/01','06/01','25/04','01/05','02/06','15/08','01/11','08/12','25/12','26/12'
		,date('d/m',easter_date(reqVar('anno'))+24*3600));
	//dbg_(print_r($aFest,1));
	foreach($aUtentiRint as $idOp => $aOpX) {
		if ($aOpX['ric_ope']!='ope') continue;
		$sTesto .= $aOpX['nome'];
		$OK = " - tutto OK";
		for($dataX=$dataIniz+3600*12; $dataX < ($dataFine+3600*24); $dataX+=3600*24) {
			if (date('N',$dataX)>5 ) continue;
			if (in_array(date('d/m',$dataX),$aFest)) continue;
			if (!isset($aOre[$idOp][date('Y-m-d',$dataX)])) {
				$sTesto .= "\r\n".date("d/m/Y",$dataX)." manca registrazione";
				$OK = "";
				continue;
			}
			if ($aOre[$idOp][date('Y-m-d',$dataX)] < 7.5) {
				$sTesto .= "\r\n".date("d/m/Y",$dataX)." ore: ".((float)$aOre[$idOp][date('Y-m-d',$dataX)])
					." meno di 7,5 ore";
				$OK = "";
			} elseif ($aOre[$idOp][date('Y-m-d',$dataX)] > 8) {
				$sTesto .= "\r\n".date("d/m/Y",$dataX)." ore: ".((float)$aOre[$idOp][date('Y-m-d',$dataX)])
					." piu' di 8 ore";
				$OK = "";
			} 
		}
		$sTesto .= $OK."\r\n\r\n";
	}
	
	return "Totale ore: $tot";
}